import requests

class HttpUtil():
    def __init__(self, baseURL, statement=True):
        self.baseURL = baseURL
        if statement: self.statement()

    def get(self, path, params={}, timeout=30, headers={}):
        url = self.baseURL + path
        try:
            r = requests.get(url, params=params, timeout=timeout, headers=headers)
            r.raise_for_status()
            r.encoding = r.apparent_encoding
            return r.text
        except Exception as e:
            print(f'请求{url}失败：{e}')
            return False

    def post(self, path, data={}, timeout=3, headers={}):
        url = self.baseURL + path
        try:
            r = requests.post(url, data, timeout=timeout, headers=headers)
            r.raise_for_status()
            r.encoding = r.apparent_encoding
            return r.text
        except:
            return ''

    def statement(self):
        statement = f'{self.__class__.__name__}\n' \
                         '作者: 梦辰雪（Cui Mengchao）\n' \
                         '版本号: v1.0.0\n' \
                         '更新时间: 2023-05-10\n' \
                         'gitee: https://gitee.com/mcxgitee\n'
        print(f'\033[1;33m{statement}\033[0m\n')

# region 单例
instance = None

def initInstance(baseURL, statement=True):
    global instance
    if not instance:
        instance = HttpUtil(baseURL, statement=statement)
    return instance
# endregion